/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package _4_Mapper;

import _1_Bean.Load1PostBean;
import _1_Bean.LoadPostListBean2;
import _5_DB.ConnectDB;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Lc.Danh
 */
public class LoadPostMapper {
    //Load danh sach bai viet
    public static LoadPostListBean2 loadlist(int uID)
    {
        LoadPostListBean2 o=null ;
        String dsbaiviet="";
        
        try{
            
            ConnectDB db = new ConnectDB();
            Connection con = db.getConnection();
            
            CallableStatement proc = con.prepareCall("{ call procLayDanhSachBaiViet(?) }");          //tên procedure và tham số
            
            proc.setInt(1, uID);      //tham số 1
            //doc ra thi can resultset, khong thi executeNonQuery(cai tra ve kieu Int)
            ResultSet rs = proc.executeQuery();                             //Trả về result set
            
            o = new LoadPostListBean2();   
            /*
                o.setpID(rs.getInt("pID"));
                o.setUserID(rs.getInt("userID"));
                o.setTitle(rs.getString("title"));
                o.setContents(rs.getString("contents"));
                o.setLiked(rs.getString("liked"));
                o.setCreatedate(rs.getString("createdate"));
                o.setModifydate(rs.getString("modifydate"));
                o.setTag(rs.getString("tag"));
            */
            while (rs.next())
            {
                int pID=rs.getInt("pID");
                String title=rs.getString("title");
                String content=rs.getString("contents");
                String liked=rs.getString("liked");
                String createdate=rs.getString("createdate");
                
                dsbaiviet += "<a href='./Post?p=" + pID;      //Nơi dẫn link đến bài viết
                dsbaiviet += "'>";
                dsbaiviet += "<h1>"+title + "</h1><br/>";
                dsbaiviet += "</a>";
                if (content.length()>400)
                dsbaiviet += content.substring(0,400);
                else
                    dsbaiviet +=content;        
                dsbaiviet += "<br/>Ngày tạo: " + createdate + " | ";
                if (liked != null)
                    dsbaiviet += "Liked: " + liked.split(";").length + "<br/>";

                dsbaiviet += "<br/><br/><br/>";                         //Phân cách giữa các bài viết
            }
            o.setContents(dsbaiviet);
            
            con.close();
        } 
        catch(SQLException e)
        {
            System.out.println("Loi: "+e.getMessage());
        }
        
        return o;
    }
    
    //Load 1 bai viet
    public static Load1PostBean load1post(int pID)
    {
        Load1PostBean o=null ;
        
        try{
            ConnectDB db = new ConnectDB();
            Connection con = db.getConnection();
            
            CallableStatement proc = con.prepareCall("{ call procLay1BaiViet(?) }");          //tên procedure và tham số
            
            proc.setInt(1, pID);      //tham số 1
            //doc ra thi can resultset, khong thi executeNonQuery(cai tra ve kieu Int)
            ResultSet rs = proc.executeQuery();                             //Trả về result set
            
            o = new Load1PostBean();   
            if (rs.next())
            {
                o.setpID(rs.getInt("pID"));
                o.setUserID(rs.getInt("userID"));
                o.setTitle(rs.getString("title"));
                o.setContents(rs.getString("contents"));
                o.setLiked(rs.getString("liked"));
                o.setCreatedate(rs.getString("createdate"));
                o.setModifydate(rs.getString("modifydate"));
                o.setTag(rs.getString("tag"));
                o.setNick(rs.getString("nick"));
                o.setAvatar(rs.getString("avatar"));
                o.setName(rs.getString("name"));
            }
            
            con.close();
        } 
        catch(SQLException e)
        {
            System.out.println("Loi: "+e.getMessage());
        }
        
        return o;
    }
    
}
